package cn.aitrox.ry.util;

import java.util.Arrays;
import java.util.List;

public class RectangleUtil {

    private RectangleUtil() {

    }

    /**
     * 获取两个矩形的交并比
     */
    public static double getNuRate(double x1, double y1, double x2, double y2,
                                   double x3, double y3, double x4, double y4) {

        // 判断是否相交
        if (!RectangleUtil.intersect(x1, y1, x2, y2, x3, y3, x4, y4)) {
            return 0;
        }

        double xIntersect = getIntersectLength(x1, x2, x3, x4);
        double yIntersect = getIntersectLength(y1, y2, y3, y4);
        if (xIntersect != 0 && yIntersect != 0) {
            double area1 = getArea(x1, y1, x2, y2);
            double area2 = getArea(x3, y3, x4, y4);
            double intersectArea = Math.abs(xIntersect * yIntersect);
            return intersectArea / (area1 + area2 - intersectArea);
        }
        return 0;
    }

    /**
     * 获取两个矩形的包含比
     */
    public static double getIncludeRate(double x1, double y1, double x2, double y2,
                                        double x3, double y3, double x4, double y4) {

        // 判断是否相交
        if (!RectangleUtil.intersect(x1, y1, x2, y2, x3, y3, x4, y4)) {
            return 0;
        }

        double xIntersect = getIntersectLength(x1, x2, x3, x4);
        double yIntersect = getIntersectLength(y1, y2, y3, y4);

        if (xIntersect == 0 || yIntersect == 0) {
            return 0;
        }

        double intersectArea = Math.abs(xIntersect * yIntersect);

        double area1 = getArea(x1, y1, x2, y2);
        double area2 = getArea(x3, y3, x4, y4);

        if (0 == area1 || area2 == 0) {
            return 0;
        }

        return intersectArea / Math.min(area1, area2);
    }


    /**
     * 获取矩形面积
     */
    public static double getArea(double x1, double y1, double x2, double y2) {
        return Math.abs((x1 - x2) * (y1 - y2));
    }

    public static boolean isFlatIntersect(double x1, double x2, double x3, double x4, boolean excludedSpecialPoint) {

        if (excludedSpecialPoint) {
            return Math.max(Math.min(x1, x2), Math.min(x3, x4)) < Math.min(Math.max(x1, x2), Math.max(x3, x4));
        }
        return Math.max(Math.min(x1, x2), Math.min(x3, x4)) <= Math.min(Math.max(x1, x2), Math.max(x3, x4));
    }

//    public static double getIntersectLength(double x1, double x2, double x3, double x4, boolean excludedSpecialPoint) {
//        if (isFlatIntersect(x1, x2, x3, x4, excludedSpecialPoint)) {
//            return Math.abs(Math.max(Math.min(x1, x2), Math.min(x3, x4)) - Math.min(Math.max(x1, x2), Math.max(x3, x4)));
//        }
//        return 0;
//    }

    public static double getIntersectLength(double x1, double x2, double x3, double x4) {
        // 非法判断
        if (x1 == x2 || x3 == x4) {
            return 0;
        }

        // 包含的情况
        if (Math.min(x1, x2) == Math.min(x3, x4) || Math.max(x1, x2) == Math.max(x3, x4)) {
            return Math.min(Math.abs(x1 - x2), Math.abs(x3 - x4));
        }

        // 相交的情况
        return Math.abs(Math.max(Math.min(x1, x2), Math.min(x3, x4)) - Math.min(Math.max(x1, x2), Math.max(x3, x4)));
    }

    /**
     * 根据矩形对角线的两个点，获取左上和右下两个点
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @return
     */
    public static List<Double> getLeftTopAndRightButtom(double x1, double y1, double x2, double y2) {
        double minX = Math.min(x1, x2);
        double maxX = Math.max(x1, x2);
        double minY = Math.min(y1, y2);
        double maxY = Math.max(y1, y2);

        return Arrays.asList(minX, minY, maxX, maxY);
    }

    public static List<Double> getLeftTopAndWightHeiht(double x1, double y1, double x2, double y2) {
        double minX = Math.min(x1, x2);
        double maxX = Math.max(x1, x2);
        double minY = Math.min(y1, y2);
        double maxY = Math.max(y1, y2);
        return Arrays.asList(minX, minY, maxX - minX, maxY - minY);
    }

    /**
     * 判读两个矩形是否相交（x和y轴都相交，包括完全一致和包含）
     */
    public static Boolean intersect(double x1, double y1, double x2, double y2,
                                    double xx1, double yy1, double xx2, double yy2) {
        if (intersectOnAxis(x1, x2, xx1, xx2) && intersectOnAxis(y1, y2, yy1, yy2)) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否在同一轴上相交
     */
    public static Boolean intersectOnAxis(double x1, double x2, double xx1, double xx2) {
        double minX = Math.min(x1, x2);
        double maxX = Math.max(x1, x2);

        double minXX = Math.min(xx1, xx2);
        double maxXX = Math.max(xx1, xx2);

        if (minX <= minXX && maxX >= minXX) {
            return true;
        }

        if (minXX <= minX && maxXX >= minX) {
            return true;
        }

        return false;
    }


}
